package com.cskaoyan.mav;

import java.sql.*;

/**
 * @ClassName JdbcDemo1
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/2/8 14:44
 * @Version V1.0
 **/
public class JdbcDemo1 {

    public static void main(String[] args) {
        //DriverManager.registerDriver(new Driver());
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb4", "root", "123456");
            //利用connection创建和数据库交互的对象，用来发送sql语句
            preparedStatement = connection.prepareStatement("select * from account");
            //执行完sql语句之后，会返回查询的结果，被封装在了结果集中
            resultSet = preparedStatement.executeQuery();
            resultSet.next();
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            double money = resultSet.getDouble("money");
            System.out.println(id + " " + name + " " + money);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(connection != null){
                    connection.close();
                }
                if(preparedStatement != null){
                    preparedStatement.close();
                }
                if(resultSet != null){
                    resultSet.close();
                }
            }catch (Exception e1){

            }
        }
    }
}
